//
//  EditImageView.m
//  MPTLive
//
//  Created by HouGeng on 15/11/17.
//  Copyright © 2015年 Jeakin. All rights reserved.
//

#import "EditImageView.h"

/// 通过RGBA设置颜色，使用0x格式，如：RGBAAllColor(0xAABBCC, 0.5);
#define RGBAAllColor(rgb, a) [UIColor colorWithRed:((float)((rgb & 0xFF0000) >> 16))/255.0  \
green:((float)((rgb & 0xFF00) >> 8))/255.0     \
blue:((float)(rgb & 0xFF))/255.0              \
alpha:(a)/1.0]

@interface EditImageView()
<UIScrollViewDelegate>

/// 滑动效果
@property (nonatomic, strong) UIScrollView *scrV;

///
@property (nonatomic, strong) UIImageView *imageV;

/// 取消按钮
@property (nonatomic, strong) UIButton *butQuXiao;

/// 选择按钮
@property (nonatomic, strong) UIButton *butXuanZhe;

//
@property (nonatomic) CGSize originalImageViewSize;


@end

@implementation EditImageView

- (id)initWithFrame:(CGRect)frame Editiamge:(UIImage *)image
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor blackColor];
        
        /// 图片
        UIImage *images;
        if (image.size.width <= image.size.height)
        {
            /// 图片
            images = [self imageWithImage:image scaledToSize:CGSizeMake(self.frame.size.width,self.frame.size.width / image.size.width *image.size.height)];
            _imageYuanTu = images;
        }
        else
        {
            /// 图片
            images = [self imageWithImage:image scaledToSize:CGSizeMake(self.frame.size.width / image.size.height *image.size.width,self.frame.size.width)];
            _imageYuanTu = images;
        }

        
        /// 滑动
        _scrV = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
                                                               (self.frame.size.height - [UIScreen mainScreen].bounds.size.width) / 2.0,
                                                               [UIScreen mainScreen].bounds.size.width,
                                                               [UIScreen mainScreen].bounds.size.width)];
        [_scrV setBounces:NO];
        _scrV.clipsToBounds = NO;
        [_scrV setShowsHorizontalScrollIndicator:NO];
        [_scrV setShowsVerticalScrollIndicator:NO];
        _scrV.delegate = self;
        [self.scrV setContentSize:images.size];
        _scrV.maximumZoomScale = 2.5;
        [self addSubview:self.scrV];
        
        _imageV = [[UIImageView alloc] initWithImage:images];
        _imageV.userInteractionEnabled = YES;
        [self.scrV addSubview:_imageV];
        
        /// 遮罩上
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                 0,
                                                                 self.frame.size.width,
                                                                 (self.frame.size.height - self.frame.size.width) / 2.0)];
        view1.backgroundColor = [UIColor colorWithWhite:0 alpha:.5];
        [self addSubview:view1];
        
        /// 遮罩下
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                 self.frame.size.height - (self.frame.size.height - self.frame.size.width) / 2.0,
                                                                 self.frame.size.width,
                                                                 (self.frame.size.height - self.frame.size.width) / 2.0)];
        view2.backgroundColor = [UIColor colorWithWhite:0 alpha:.5];
        [self addSubview:view2];
        
        
        /// 下面的横条
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 53, self.frame.size.width, 53)];
        view.backgroundColor = RGBAAllColor(0x282828, 1);
        [self addSubview:view];
        
        _butQuXiao = [UIButton buttonWithType:UIButtonTypeCustom];
        _butQuXiao.frame = CGRectMake(0, self.frame.size.height - 53, 100, 53);
        [_butQuXiao setTitleColor:RGBAAllColor(0xFFFFFF, 1) forState:UIControlStateNormal];
        [_butQuXiao addTarget:self action:@selector(butClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_butQuXiao setTitle:@"  取消" forState:UIControlStateNormal];
        _butQuXiao.titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_butQuXiao];
        
        
        _butXuanZhe = [UIButton buttonWithType:UIButtonTypeCustom];
        _butXuanZhe.frame = CGRectMake(self.frame.size.width - 100, self.frame.size.height - 53, 100, 53);
        [_butXuanZhe setTitleColor:RGBAAllColor(0xFFFFFF, 1) forState:UIControlStateNormal];
        [_butXuanZhe addTarget:self action:@selector(butClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_butXuanZhe setTitle:@"选择  " forState:UIControlStateNormal];
        _butXuanZhe.titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_butXuanZhe];
        
        
        float _imageScale = self.scrV.frame.size.width / _imageYuanTu.size.width;
        _originalImageViewSize = CGSizeMake(_imageYuanTu.size.width*_imageScale, _imageYuanTu.size.height*_imageScale);
    }
    return self;
}

/// 获取图片
- (UIImage *)getEditImage
{
    CGImageRef imgrefout = CGImageCreateWithImageInRect([_imageYuanTu CGImage],
                                                        [self finishCropping]);
    UIImage *img_ret = [[UIImage alloc]initWithCGImage:imgrefout];
    return img_ret;
}

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (void)butClicked:(UIButton *)but
{
    if (_butQuXiao == but)
    {
        [self removeFromSuperview];
    }
    else
    {
        self.block(1);
    }
}
//告诉scrollview要缩放的是哪个子控件
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
     return _imageV;
}

- (CGRect)finishCropping
{
    return CGRectMake(_scrV.contentOffset.x *[UIScreen mainScreen].scale / self.scrV.zoomScale,
                      _scrV.contentOffset.y *[UIScreen mainScreen].scale / self.scrV.zoomScale,
                      _scrV.frame.size.width *[UIScreen mainScreen].scale / self.scrV.zoomScale,
                      _scrV.frame.size.height *[UIScreen mainScreen].scale / self.scrV.zoomScale);
}

@end
